package com.pb.metadata.controller;

import com.pb.infra.oth.entity.common.Page;
import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.oth.util.Constant;
import com.pb.infra.oth.util.JwtTokenConfig;
import com.pb.infra.oth.util.SysLog;
import com.pb.infra.oth.util.UserInfo;
import com.pb.metadata.service.CoefficientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 系数管理
 * @author lzh
 * @date 2022/12/23
 */
@Slf4j
@RestController
@RequestMapping("v1/metadata/paraTree")
public class CoefficientController {

    /**
     * 业务逻辑对象
     */
    @Resource
    private CoefficientService coefficientService;

    /**
     * 日志操作表
     */
    private static final String TABLE_NAME = "PARAM_PARA_TREE";

    /**
     * 日志操作功能名
     */
    private static final String OPERATION_MENU = "系数管理";

    @Resource
    private JwtTokenConfig jwtTokenConfig;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 查询系数树
     *
     * @return Result 返回数据
     */
    @GetMapping("selectPara")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result selectPara(@RequestParam Map<String, Object> map) {
        return coefficientService.selectPara(map);
    }

    /**
     * 保存系数
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("insert")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.ADD, menuName = OPERATION_MENU)
    public Result insert(@RequestBody Map<String, Object> paramMap, HttpServletRequest request) {
        return coefficientService.insert(paramMap, request);
    }

    /**
     * 修改系数
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("update")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result update(@RequestBody Map<String, Object> paramMap){
        return coefficientService.update(paramMap);
    }

    /**
     * 删除系数
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("delete")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.DEL, menuName = OPERATION_MENU)
    public Result delete(@RequestBody Map<String, Object> paramMap) {
        return coefficientService.delete(paramMap);
    }

    /**
     * 校验系数名字是否存在
     *
     * @return Result 返回数据
     */
    @GetMapping("validUniqueName")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result validUniqueName(@RequestParam Map<String, Object> map) {
        return coefficientService.validUniqueName(map);
    }

    /**
     * 查询系数值
     * @param map
     * @return
     */
    @GetMapping("selectParaVal")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result selectParaVal(@RequestParam Map<String, Object> map, Page page) {
        return coefficientService.selectParaVal(map, page);
    }

    /**
     * 保存系数值
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("insertParaVal")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.ADD, menuName = OPERATION_MENU)
    public Result insertParaVal(@RequestBody Map<String, Object> paramMap, HttpServletRequest request) {
        return coefficientService.insertParaVal(paramMap, request);
    }

    /**
     * 修改系数值
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("updateParaVal")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result updateParaVal(@RequestBody Map<String, Object> paramMap){
        return coefficientService.updateParaVal(paramMap);
    }

    /**
     * 删除系数值
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("deleteParaVal")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.DEL, menuName = OPERATION_MENU)
    public Result deleteParaVal(@RequestBody Map<String, Object> paramMap) {
        return coefficientService.deleteParaVal(paramMap);
    }

    /**
     * 查询科目树
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("getSubjTree")
    public Result getSubjTree(@RequestBody Map<String, Object> paramMap) {
        return coefficientService.getSubjTree(paramMap);
    }

    /**
     * 查询产品树
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("getPrdtTree")
    public Result getPrdtTree(@RequestBody Map<String, Object> paramMap) {
        return coefficientService.getPrdtTree(paramMap);
    }

    /**
     * 查询获取维度树
     * @param paramMap 请求参数{corpId 法人ID   dimNo 维度ID}
     * @return Result 返回数据
     */
    @PostMapping("getDimTree")
    public Result getDimTree(@RequestBody Map<String, String> paramMap, HttpServletRequest request) {
        String token = request.getHeader(jwtTokenConfig.getHeader());
        UserInfo userInfo = (UserInfo) redisTemplate.opsForValue().get(token);
        paramMap.put("corpId",userInfo.getCorpId());
        return coefficientService.getDimTree(paramMap);
    }

}
